﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JXT.PrimaryKey.Batman.Core.WcfService;

namespace JXT.PrimaryKey.Batman.WcfService.Services.Extensions
{
    public static class ConvertExtensions
    {
        public static int ToInt32(this string s)
        {
            int result = 0;
            if (!Int32.TryParse(s, out result))
            {
                throw new WebHttpException(WebHttpErrorCode.ObjectConvertError,
                    "{0}无法转换到Int32类型.", s);
            }
            return result;
        }

        public static Guid ToGuid(this string id)
        {
            Guid result = Guid.Empty;
            if (!Guid.TryParse(id, out result))
            {
                throw new WebHttpException(WebHttpErrorCode.ObjectConvertError,
                    "{0}无法转换到Guid类型.", id);
            }
            return result;
        }

        public static DateTime ToDateTime(this string timeString)
        {
            DateTime result = DateTime.MinValue;
            if (!DateTime.TryParse(timeString, out result))
            {
                throw new WebHttpException(WebHttpErrorCode.ObjectConvertError,
                       "{0}无法转换到DateTime类型.", timeString);
            }
            return result;
        }
    }
}
